<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章标题" prop="techniqueTitle">
        <el-input
          v-model="queryParams.techniqueTitle"
          placeholder="请输入文章标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论用户" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="评论状态" clearable>
          <el-option label="正常" value="1" />
          <el-option label="已删除" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="评论时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fishing:comment:remove']"
        >批量删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评论ID" align="center" prop="id" width="80" />
      <el-table-column label="文章标题" align="center" prop="techniqueTitle" :show-overflow-tooltip="true" width="200" />
      <el-table-column label="评论用户" align="center" prop="userName" width="120" />
      <el-table-column label="用户头像" align="center" prop="userAvatar" width="80">
        <template #default="scope">
          <el-avatar :size="40" :src="scope.row.userAvatar" v-if="scope.row.userAvatar">
            <img src="@/assets/images/profile.jpg" />
          </el-avatar>
          <el-avatar :size="40" v-else>
            <el-icon><User /></el-icon>
          </el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="评论内容" align="center" prop="content" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column label="父评论" align="center" prop="parentContent" :show-overflow-tooltip="true" width="150">
        <template #default="scope">
          <span v-if="scope.row.parentId">{{ scope.row.parentContent || '原评论已删除' }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="点赞数" align="center" prop="likeCount" width="80" />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.status === '1' ? 'success' : 'danger'">
            {{ scope.row.status === '1' ? '正常' : '已删除' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="评论时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看</el-button>
          <el-button 
            link 
            type="primary" 
            icon="ChatDotRound" 
            @click="handleReply(scope.row)"
            v-if="scope.row.status === '1'"
          >回复</el-button>
          <el-button 
            link 
            type="warning" 
            icon="Edit" 
            @click="handleStatusChange(scope.row)"
            v-hasPermi="['fishing:comment:edit']"
          >
            {{ scope.row.status === '1' ? '删除' : '恢复' }}
          </el-button>
          <el-button 
            link 
            type="danger" 
            icon="Delete" 
            @click="handleDelete(scope.row)" 
            v-hasPermi="['fishing:comment:remove']"
          >彻底删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看评论详情对话框 -->
    <el-dialog title="评论详情" v-model="viewOpen" width="800px" append-to-body>
      <div v-if="viewData">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="评论ID">{{ viewData.id }}</el-descriptions-item>
          <el-descriptions-item label="文章标题">{{ viewData.techniqueTitle }}</el-descriptions-item>
          <el-descriptions-item label="评论用户">{{ viewData.userName }}</el-descriptions-item>
          <el-descriptions-item label="用户邮箱">{{ viewData.userEmail || '-' }}</el-descriptions-item>
          <el-descriptions-item label="点赞数">{{ viewData.likeCount }}</el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag :type="viewData.status === '1' ? 'success' : 'danger'">
              {{ viewData.status === '1' ? '正常' : '已删除' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="评论时间" :span="2">{{ parseTime(viewData.createTime) }}</el-descriptions-item>
        </el-descriptions>
        <div style="margin-top: 20px;" v-if="viewData.parentId">
          <h4>回复的评论：</h4>
          <div style="background: #f5f5f5; padding: 10px; border-radius: 4px;">
            {{ viewData.parentContent || '原评论已删除' }}
          </div>
        </div>
        <div style="margin-top: 20px;">
          <h4>评论内容：</h4>
          <div style="border: 1px solid #ddd; padding: 15px; border-radius: 4px; min-height: 100px;">
            {{ viewData.content }}
          </div>
        </div>
      </div>
    </el-dialog>

    <!-- 回复评论对话框 -->
    <el-dialog title="回复评论" v-model="replyOpen" width="600px" append-to-body>
      <div v-if="replyData">
        <div style="background: #f5f5f5; padding: 10px; border-radius: 4px; margin-bottom: 15px;">
          <strong>原评论：</strong>{{ replyData.content }}
        </div>
        <el-form ref="replyRef" :model="replyForm" :rules="replyRules" label-width="80px">
          <el-form-item label="回复内容" prop="content">
            <el-input
              v-model="replyForm.content"
              type="textarea"
              :rows="4"
              placeholder="请输入回复内容"
            />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitReply">确 定</el-button>
          <el-button @click="replyOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Comment">
import { listComment, getComment, delComment, updateCommentStatus, delComments, replyComment } from "@/api/fishing/comment";
import { User } from '@element-plus/icons-vue';

const { proxy } = getCurrentInstance();

const commentList = ref([]);
const viewOpen = ref(false);
const replyOpen = ref(false);
const viewData = ref(null);
const replyData = ref(null);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const dateRange = ref([]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    techniqueTitle: null,
    userName: null,
    status: null
  },
  replyForm: {
    content: null,
    parentId: null,
    techniqueId: null
  },
  replyRules: {
    content: [
      { required: true, message: "回复内容不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, replyForm, replyRules } = toRefs(data);

/** 查询评论列表 */
function getList() {
  loading.value = true;
  const params = proxy.addDateRange(queryParams.value, dateRange.value);
  listComment(params).then(response => {
    commentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 查看按钮操作 */
function handleView(row) {
  getComment(row.id).then(response => {
    viewData.value = response.data;
    viewOpen.value = true;
  });
}

/** 回复按钮操作 */
function handleReply(row) {
  replyData.value = row;
  replyForm.value = {
    content: null,
    parentId: row.id,
    techniqueId: row.techniqueId
  };
  replyOpen.value = true;
}

/** 提交回复 */
function submitReply() {
  proxy.$refs["replyRef"].validate(valid => {
    if (valid) {
      // 创建提交数据的副本，排除日期字段
      const submitData = { ...replyForm.value };
      delete submitData.createTime;
      delete submitData.updateTime;
      
      replyComment(submitData).then(response => {
        proxy.$modal.msgSuccess("回复成功");
        replyOpen.value = false;
        getList();
      });
    }
  });
}

/** 状态修改 */
function handleStatusChange(row) {
  let text = row.status === "1" ? "删除" : "恢复";
  let newStatus = row.status === "1" ? "0" : "1";
  proxy.$modal.confirm('确认要"' + text + '"该评论吗？').then(function() {
    return updateCommentStatus(row.id, newStatus);
  }).then(() => {
    row.status = newStatus;
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(() => {});
}

/** 删除按钮操作 */
function handleDelete(row) {
  const commentIds = row.id || ids.value;
  proxy.$modal.confirm('是否确认彻底删除评论编号为"' + commentIds + '"的数据项？此操作不可恢复！').then(function() {
    return delComment(commentIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>

<style scoped>
.el-avatar {
  border: 1px solid #ddd;
}
</style>